package com.yuchting.dinneronline.server;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONObject;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Order {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long m_time = System.currentTimeMillis();
	
	@Persistent
	private String m_username = "";
	
	@Persistent
	private String	m_dinnerName = "";
	
	@Persistent
	private double m_pay = 9.5f;
	
	@Persistent
	private boolean m_commented = false;
	
	public Order(){}
	
	public Order(User _user,Dinner _dinner){
		m_username = _user.getName();		
		m_dinnerName = _dinner.getName();
		
		m_pay = _dinner.getPrice();
	}
	
	// sets and gets function
	//
	public long getTime(){return m_time;}
	
	public void setUserName(String _name){m_username = _name;}
	public String getUserName(){return m_username;}
	
	public void setDinnerName(String _dinner){m_dinnerName = _dinner;}
	public String getDinnerName(){return m_dinnerName;}

	public double getPayMoney(){return m_pay;}
	public void setPayMoney(double _money){m_pay = _money;}
	
	public boolean isCommented(){return m_commented;}
	
	public JSONObject toJSON(){
		
		JSONObject t_result = new JSONObject();
		try{
			t_result.put("time",m_time);
			t_result.put("user",m_username);
			t_result.put("dinner",m_dinnerName);
			t_result.put("pay",m_pay);
			t_result.put("commented",m_commented);
		}catch(Exception e){
			System.err.println("Order toJSON Error:");
			e.printStackTrace(System.err);
		}
		
		return t_result;
	}
	
	public void fromJSON(JSONObject _json){
		try{
			
			m_time 			= _json.getLong("time");
			m_username 		= _json.getString("user");
			m_dinnerName 	= _json.getString("dinner");
			m_pay 			= _json.getDouble("pay");
			m_commented		= _json.getBoolean("commented");
			
		}catch(Exception e){
			System.err.println("Order fromJSON Error:");
			e.printStackTrace(System.err);
		}
	}
	
}
